//
//  MooreDollViewController.m
//  MooreDoll
//
//  Created by Joe on 2014/11/24.
//  Copyright (c) 2014年 Tank. All rights reserved.
//

#import "MooreDollViewController.h"

@implementation MooreDollViewController
{
    NSInteger fiendFriendResultsCount;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    //檢查連線
    self.navigationController.interactivePopGestureRecognizer.enabled = NO;
    
}

-(void)viewWillAppear:(BOOL)animated
{
    
    [self checkConnect];
}

-(void)checkConnect
{
    
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults]objectForKey:@"password"]);
    NSLog(@"%i",[[[NSUserDefaults standardUserDefaults]objectForKey:@"password"] isEqualToString:@""]);

    //檢查有沒有儲存帳號密碼 且密碼不是未設定
    if ([[NSUserDefaults standardUserDefaults]objectForKey:@"account"]!=nil && [[NSUserDefaults standardUserDefaults]objectForKey:@"password"] != nil && ![[[NSUserDefaults standardUserDefaults]objectForKey:@"password"] isEqualToString:@"01234"]) {

        //檢查目前網路狀態
        if ([XMPPSharedInstance sharedInstance].state == 0) {
            
            //這邊註冊了登入後的 delegate
            [XMPPSharedInstance sharedInstance].stateDelegate = self;
            
            //執行登入
            [[XMPPSharedInstance sharedInstance] loginXMPP];
        }
        
    }else
    {
        //如果沒有儲存帳號 就轉到登入畫面
        LoginOrRegistViewController *loginVC = [self.storyboard instantiateViewControllerWithIdentifier:@"LoginOrRegistViewController"];
        [self.navigationController pushViewController:loginVC animated:NO];
        
    }

}
- (IBAction)goTest:(id)sender {
    //拿取聯絡人
    [[GetAddressBook sharedInstance]addressBookGetAuthorizationStatus];
    //檢查聯絡人取得的數量
    if ([GetAddressBook sharedInstance].MA_UserAddressBook.count > 0) {
        
        [FiendFriend sharedInstance].delegate = self;
        //取的有在使用的聯絡人 jid
        [[FiendFriend sharedInstance]searchFreindForServer:[GetAddressBook sharedInstance].MA_UserAddressBook];
    }

}

- (IBAction)openDrawer:(id)sender {
    [self.menuContainerViewController toggleLeftSideMenuCompletion:nil];
}
- (IBAction)AddMooreDoll:(id)sender {
    AddMooreDollViewController *addMooreDollVC = [self.storyboard instantiateViewControllerWithIdentifier:@"AddMooreDollViewController"];
    [self.navigationController pushViewController:addMooreDollVC animated:YES];
}


#pragma mark connectStateDelegate 連線的delegate
-(void)connectState:(int)state
{
    //因為這邊delegate 是在背景執行時觸發 但因為我們有做切換畫面 所以要回到主線程 不然會出問題
    dispatch_async(dispatch_get_main_queue(), ^{
        
        switch (state) {
            case 0:
                NSLog(@"連線中斷");
                break;
            case 1:
                NSLog(@"進行連線");
                break;
            case 2:
                NSLog(@"連線成功");
                break;
            default:
                break;
        }
    });

}

#pragma Tank FiendFriendDelegate 取得朋友狀態列表的delegate
-(void)getVcardResult:(id)responseObject
{
    if ([responseObject count]>0) {
        NSLog(@"%@",responseObject);
    }else
    {
        NSLog(@"沒有");
    }
}
@end
